package com.dmxh.controller;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradeQueryModel;
import com.alipay.api.domain.AlipayTradeRefundModel;
import com.alipay.api.domain.RefundGoodsDetail;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.request.AlipayTradeQueryRequest;
import com.alipay.api.request.AlipayTradeRefundRequest;
import com.alipay.api.response.AlipayTradeQueryResponse;
import com.alipay.api.response.AlipayTradeRefundResponse;
import com.dmxh.dto.PayDto;
import com.dmxh.result.Result;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/pay")
public class PayController {

    @Value("${alipay.serverUrl}")
    private String serverUrl;

    @Value("${alipay.appId}")
    private String appId;

    @Value("${alipay.privateKey}")
    private String privateKey;

    @Value("${alipay.format}")
    private String format;

    @Value("${alipay.charset}")
    private String charset;

    @Value("${alipay.alipayPublicKey}")
    private String alipayPublicKey;

    @Value("${alipay.signType}")
    private String signType;

    //@RequestParam String subject, @RequestParam String outTradeNo, @RequestParam String totalAmount
    @GetMapping("/alipay")//付款
    public String alipay(PayDto payDto) {// TODO: 2024/5/29 Body是解析json参数的如果不是json参数可以不用加那个注解
        //获得初始化的AlipayClient
        AlipayClient alipayClient = new DefaultAlipayClient(
                serverUrl,
                appId,
                privateKey,
                format,
                charset,
                alipayPublicKey,
                signType);
        //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setNotifyUrl("http://wifbej.natappfree.cc/ny/notifyUrl");//异步通知支付是否成功
        alipayRequest.setReturnUrl("http://127.0.0.1/paysuccess.html");//同步请求  支付成功后跳转回到商户页面http://127.0.0.1/paysuccess.html

        String body = "FAST_INSTANT_TRADE_PAY";//签名

        try {
            alipayRequest.setBizContent("{\"out_trade_no\":\"" + payDto.getOutTradeNo() + "\","
                    + "\"total_amount\":\"" + payDto.getTotalAmount() + "\","
                    + "\"subject\":\"" + payDto.getSubject() + "\","
                    + "\"body\":\"" + body + "\","
                    + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

            //请求
            String result = alipayClient.pageExecute(alipayRequest).getBody();

            //输出
            System.out.println(result);
            return result;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }


    //查询
    @GetMapping("/query")
    private Result query(String outTradeNo) {
        // 初始化SDK
        AlipayClient alipayClient = new DefaultAlipayClient(
                serverUrl,
                appId,
                privateKey,
                format,
                charset,
                alipayPublicKey,
                signType);
        // 构造请求参数以调用接口
        AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
        AlipayTradeQueryModel model = new AlipayTradeQueryModel();

        // 设置订单支付时传入的商户订单号
        model.setOutTradeNo(outTradeNo);
        request.setBizModel(model);
        AlipayTradeQueryResponse response = null;
        try {
            response = alipayClient.execute(request);
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        System.out.println(response.getBody());

        if (response.isSuccess()) {
            System.out.println("调用成功");

            return Result.ok().setData(response.getBody());
        } else {
            System.out.println("调用失败");
            return Result.fail();
        }
    }

    //退款
    @GetMapping("/refund")/*必选项:  金额  订单编号   退款请求号(如果多次退款)*/
    public Result refund(String RefundAmount, String OutTradeNo, String outRequestNo) {
        // 初始化SDK
        AlipayClient alipayClient = new DefaultAlipayClient(
                serverUrl,
                appId,
                privateKey,
                format,
                charset,
                alipayPublicKey,
                signType);

        // 构造请求参数以调用接口
        AlipayTradeRefundRequest request = new AlipayTradeRefundRequest();
        AlipayTradeRefundModel model = new AlipayTradeRefundModel();

        // 设置商户订单号
        model.setOutTradeNo(OutTradeNo);

        //退款金额
        model.setRefundAmount(RefundAmount);

        model.setRefundReason("正常退款");

        // 设置退款请求号
        model.setOutRequestNo(outRequestNo);//"HZ01RF001"

        request.setBizModel(model);
        AlipayTradeRefundResponse response = null;
        try {
            response = alipayClient.execute(request);
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        System.out.println(response.getBody());

        if (response.isSuccess()) {
            System.out.println("调用成功");
            return Result.ok().setData(response.getBody());
        } else {
            System.out.println("调用失败");
            return Result.fail();
        }
    }

    //退款查询
    @GetMapping("/refundQuery")
    public Result refundQuery(){
        // TODO: 2024/6/3 没手机,写不了  就是传几个参数然后查询就完了
        return Result.ok();
    }
}
